﻿using A.M.ETO;
using AutoMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using Q.M.Finance.Application.IServices;
using Q.M.Finance.Application.Services;
using Q.M.Finance.Domain.DTO;
using Q.M.Finance.Domain.Entity;
using Q.M.Finance.Infrastruecure.IRepository;
using Q.M.Finance.Infrastruecure.Repository;
using System.Linq.Expressions;

namespace Q.M.Finance.Write.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class IncomeWriteController : ControllerBase
    {
        private readonly IIncomeServices incomeServices;
        public IncomeWriteController(IIncomeServices incomeServices)
        {
            this.incomeServices = incomeServices;
        }
        /// <summary>
        /// 收入管理显示+分页+查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetincomeInfo(string? receiptid, int pageIndex, int pageSize)
        {
            var res= incomeServices.GetincomeInfo(receiptid, pageIndex, pageSize);
            return Ok(res); 
        }
        /// <summary>
        /// 添加收入管理
        /// </summary>
        /// <param name="incomeAddDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> GetincomeAddInfo(incomeAddDTO incomeAddDTO)
        {
            try
            {
                var res= await incomeServices.GetincomeAddInfo(incomeAddDTO);
                return Ok(res);
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改收入管理
        /// </summary>
        /// <param name="incomeUpdateDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<IActionResult> GetincomeUpdateInfo(incomeShowDTO incomeUpdateDTO)
        {
            var res= await incomeServices.GetincomeUpdateInfo(incomeUpdateDTO);
            return Ok(res);
        }
        /// <summary>
        /// 反填收入管理
        /// </summary>
        /// <param name="receiptid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> FindincomeInfo(string? receiptid)
        {
            var res = await incomeServices.FindincomeInfo(receiptid);
            return Ok(res);

        }
        /// <summary>
        /// 删除收入管理
        /// </summary>
        /// <param name="receiptid"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<IActionResult> Deleteincome(string? receiptid)
        {
            var res = await incomeServices.Deleteincome(receiptid);
            return Ok(res);
        }
        /// <summary>
        /// 导出
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult ExportTiExcel()
        {
            var list = incomeServices.GetShowInfor();
            var steram = new MemoryStream();
            steram.SaveAs(list);
            steram.Seek(0, SeekOrigin.Begin);
            return new FileStreamResult(steram, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
            {
                FileDownloadName = "收入信息.xlsx"
            };
        }
    }
}
